package Test;

import javafx.scene.control.ComboBox;

import javax.swing.*;
import java.awt.*;

public class ComboBoxTest {
    private static void ComTest(){
        JFrame frame = new JFrame("下拉");
        frame.setSize(400,500);
        frame.setLocation(300,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        JComboBox<String>comboBox = new JComboBox<String>();
        comboBox.addItem("选择城市");
        comboBox.addItem("北京");
        comboBox.addItem("上海");
        comboBox.addItem("广州");
        comboBox.addItem("深圳");
        JTextField textField = new JTextField(20);
        comboBox.addActionListener(e -> {
            String city = (String) comboBox.getSelectedItem();
            if ("选择城市".equals(city)){
                textField.setText("");
            }else {
                textField.setText("你选择："+city);
            }
        });
        panel.add(comboBox);
        panel.add(textField);
        frame.add(panel, BorderLayout.PAGE_START);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(ComboBoxTest::ComTest);
    }
}
